﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Sql;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WakeTheServer
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    /// 


    public partial class SetUpWindow : Window
    {
        public SetUpWindow()
        {
            InitializeComponent();
        }

        private bool IsHex(char c)
        {
            return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
        }
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.button3.Content = "bob";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            ProcessStartInfo info = new ProcessStartInfo("cmd", @"/c ping 192.168.1.1 -n 1 && arp -a");

            info.RedirectStandardOutput = true;
            info.UseShellExecute = false;
            info.CreateNoWindow = true;

            Process GetARP = new Process();
            GetARP.StartInfo = info;
            GetARP.Start();
            string res = GetARP.StandardOutput.ReadToEnd();
            ARPBox.Text = res;
            // StreamWriter sw = new StreamWriter("C:\\testlog.txt", true, Encoding.ASCII);
            // sw.Write(res);
            // sw.Close();
        }

        private void MAC_Box_Value_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !MAC_Box_TextAllowed(e.Text);
        }

        private void MAC_Box_Value_Pasting(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(String)))
            {
                String Text1 = (String)e.DataObject.GetData(typeof(String));
                if (!MAC_Box_TextAllowed(Text1))
                {
                    e.CancelCommand();
                }
            }
            else e.CancelCommand();
        }

        private Boolean MAC_Box_TextAllowed(String Text2)
        {
            return Array.TrueForAll<Char>(Text2.ToCharArray(),
                delegate(Char c) { return IsHex(c); });
        }

        private void MAC_Box_1_TextChanged(object sender, TextChangedEventArgs e)
        {
            
            
            if (MAC_Box_1.Text.Length == MAC_Box_1.MaxLength)
            {
                string hexString = MAC_Box_1.Text;
                int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
                Properties.Settings.Default.Dynamic_Button_1_Octet_1 = num;
                Properties.Settings.Default.Save();
                MAC_Box_2.Focus();
            }
        }
        private void MAC_Box_2_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (MAC_Box_2.Text.Length == MAC_Box_2.MaxLength)
            {
                string hexString = MAC_Box_2.Text;
                int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
                
                MAC_Box_3.Focus();
            }
        }
        private void MAC_Box_3_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (MAC_Box_3.Text.Length == MAC_Box_3.MaxLength)
            {
                string hexString = MAC_Box_3.Text;
                int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
                
                MAC_Box_4.Focus();
            }
        }
        private void MAC_Box_4_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (MAC_Box_4.Text.Length == MAC_Box_4.MaxLength)
            {
                string hexString = MAC_Box_4.Text;
                int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
                
                MAC_Box_5.Focus();
            }
        }
        private void MAC_Box_5_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (MAC_Box_5.Text.Length == MAC_Box_5.MaxLength)
            {
                string hexString = MAC_Box_5.Text;
                int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
                
                MAC_Box_6.Focus();
            }
        }
        private void MAC_Box_6_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (MAC_Box_6.Text.Length == MAC_Box_6.MaxLength)
            {
                string hexString = MAC_Box_6.Text;
                int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
                
                button3.Focus();
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string IpAddressString = PC_IP_Textbox.Text;
            ButtonTextBox.Text = getHost(IpAddressString);
        }

        public string getHost(string ip)
        {
            try
            {
                IPHostEntry IpEntry = Dns.GetHostEntry(ip);
                return IpEntry.HostName.ToString();
            }
            catch (SocketException e)
            {
                return ("SocketException caught!!!" + "\r\n" + "Source : " + e.Source + "\r\n" + "Message : " + e.Message);
            }
            catch (FormatException e)
            {
                return ("FormatException caught!!!" + "\r\n" + "Source : " + e.Source + "\r\n" + "Message : " + e.Message);
            }
            catch (ArgumentNullException e)
            {
                return ("ArgumentNullException caught!!!" + "\r\n" + "Source : " + e.Source + "\r\n" + "Message : " + e.Message);
            }
            catch (Exception e)
            {
                return ("Exception caught!!!" + "\r\n" + "Source : " + e.Source + "\r\n" + "Message : " + e.Message);
            }
        }
    }
}
